package com.qs.eggs.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qs.eggs.R;
import com.shizhefei.view.indicator.FixedIndicatorView;
import com.shizhefei.view.indicator.IndicatorViewPager;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/5/3.
 */

public class SortTabIndicatorAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

    private String[] tabNameS;
    private LayoutInflater inflater;
    private ArrayList<Fragment>fmList;
    private ViewPager viewPager;
    public static final int ACTION_UP=1;
    public static final int ACTION_DOWN=2;
    private Context mContext;
    public SortTabIndicatorAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    public SortTabIndicatorAdapter(ViewPager viewPager, FragmentManager fragmentManager, Context mContext, ArrayList<Fragment> fmList, String[]tabNameS) {
        super(fragmentManager);
        this.tabNameS=tabNameS;
        this.mContext=mContext;
        this.viewPager=viewPager;
        this.fmList=fmList;
        inflater = LayoutInflater.from(mContext);

    }

    @Override
    public int getCount() {
        return tabNameS.length;
    }

    @Override
    public View getViewForTab(int position, View convertView, ViewGroup container) {
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.tab_guide,
                    container, false);
        }
        final ImageView up=(ImageView)convertView.findViewById(R.id.img_up);
        final ImageView down=(ImageView)convertView.findViewById(R.id.img_down);
        final TextView textView = (TextView) convertView.findViewById(R.id.tv_tab);
        RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.rly_sort);
        textView.setText(tabNameS[position]);
        textView.setTag(ACTION_UP);

        if(position==2){
            relativeLayout.setVisibility(View.VISIBLE);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int action = (int) v.getTag();
                    if(ACTION_UP == action){
                        textView.setTag(ACTION_DOWN);
                        viewPager.setCurrentItem(2);
                        up.setImageResource(R.mipmap.icon_sort_upgrey);
                        down.setImageResource(R.mipmap.icon_sort_downred);


                    }
                    else if(ACTION_DOWN == action){
                        textView.setTag(ACTION_UP);
                        viewPager.setCurrentItem(2);
                        up.setImageResource(R.mipmap.icon_sort_upred);
                        down.setImageResource(R.mipmap.icon_sort_downgrey);

                    }
                }
            });
        }else {
            relativeLayout.setVisibility(View.GONE);

        }

        return convertView;
    }

    @Override
    public Fragment getFragmentForPage(int position) {
        // 填充Fragment界面,需要与tab数量相等
        switch (position) {
            case 0:
                return fmList.get(position);

            case 1:
                return fmList.get(position);

            case 2:
                return fmList.get(position);

            default:
                return null;

        }
    }
}
